﻿/*
COPYRIGHT (C) 2011-2012 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Web.Mvc;

namespace ScrumPilot.Helpers
{
    /*
     * come from 
     * Using 51Degrees.Mobi Foundation for accurate mobile browser detection on ASP.NET MVC 3
     * http://blog.stevensanderson.com/2010/12/17/using-51degreesmobi-foundation-for-accurate-mobile-browser-detection-on-aspnet-mvc-3/
     * and
     * A Better ASP.NET MVC Mobile Device Capabilities ViewEngine by Scott Hanselan
     * http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx
     */

    public class CustomMobileViewEngine : IViewEngine
    {
        public IViewEngine BaseViewEngine { get; private set; }
        public Func<ControllerContext, bool> IsTheRightDevice { get; private set; }
        public string PathToSearch { get; private set; }

        public CustomMobileViewEngine(Func<ControllerContext, bool> isTheRightDevice, string pathToSearch,
                                      IViewEngine baseViewEngine)
        {
            BaseViewEngine = baseViewEngine;
            IsTheRightDevice = isTheRightDevice;
            PathToSearch = pathToSearch;
        }

        public ViewEngineResult FindPartialView(ControllerContext context, string viewName, bool useCache)
        {
            if (IsTheRightDevice(context))
            {
                return BaseViewEngine.FindPartialView(context, PathToSearch + "/" + viewName, useCache);
            }
            return new ViewEngineResult(new string[] {}); //we found nothing and we pretend we looked nowhere
        }

        public ViewEngineResult FindView(ControllerContext context, string viewName, string masterName, bool useCache)
        {
            if (IsTheRightDevice(context))
            {
                return BaseViewEngine.FindView(context, PathToSearch + "/" + viewName, masterName, useCache);
            }
            return new ViewEngineResult(new string[] {}); //we found nothing and we pretend we looked nowhere
        }

        public void ReleaseView(ControllerContext controllerContext, IView view)
        {
            throw new NotImplementedException();
        }
    }

    public static class MobileHelpers
    {
        public static bool UserAgentContains(this ControllerContext c, string agentToFind)
        {
            return (c.HttpContext.Request.UserAgent.IndexOf(agentToFind, StringComparison.OrdinalIgnoreCase) > 0);
        }

        public static bool IsMobileDevice(this ControllerContext c)
        {
            return c.HttpContext.Request.Browser.IsMobileDevice || UserAgentContains(c, "iemobile");
                // Just use it for debug JS in firebug & inspect HTML -> || UserAgentContains("firefox")
        }

        public static void AddMobile<T>(this ViewEngineCollection ves, Func<ControllerContext, bool> isTheRightDevice,
                                        string pathToSearch)
            where T : IViewEngine, new()
        {
            ves.Add(new CustomMobileViewEngine(isTheRightDevice, pathToSearch, new T()));
        }

        public static void AddMobile<T>(this ViewEngineCollection ves, string userAgentSubstring, string pathToSearch)
            where T : IViewEngine, new()
        {
            ves.Add(new CustomMobileViewEngine(c => c.UserAgentContains(userAgentSubstring), pathToSearch, new T()));
        }

        public static void AddIPhone<T>(this ViewEngineCollection ves) //specific example helper
            where T : IViewEngine, new()
        {
            //ves.Add(new CustomMobileViewEngine(c => c.UserAgentContains("iPhone"), "Mobile/iPhone", new T()));
            ves.Add(new CustomMobileViewEngine(c => c.UserAgentContains("iPhone"), "Mobile", new T()));
        }

        public static void AddWindowsMobile<T>(this ViewEngineCollection ves) //specific example helper
            where T : IViewEngine, new()
        {
            ves.Add(new CustomMobileViewEngine(c => c.UserAgentContains("IEMobile"), "Mobile", new T()));
        }

        public static void AddFireBug<T>(this ViewEngineCollection ves) //Add firefox + firebug for help to debug
            where T : IViewEngine, new()
        {
            ves.Add(new CustomMobileViewEngine(c => c.UserAgentContains("firefox"), "Mobile", new T()));
        }

        public static void AddGenericMobile<T>(this ViewEngineCollection ves)
            where T : IViewEngine, new()
        {
            ves.Add(new CustomMobileViewEngine(c => c.IsMobileDevice(), "Mobile", new T()));
        }
    }
}